package cn.wd.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.wd.model.Customer;
import cn.wd.model.Goods;
import cn.wd.model.dto.GoodsDTO;
import cn.wd.service.GoodsService;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Autowired
	GoodsService goodsService;
	
	@RequestMapping("/toAddGoods.do")
	public String toAddGoods(){
		return "addGoods";
	}
	@RequestMapping("/toShowShopsGoods.do")
	public String toShowShopsGoods(){
		return "showShopsGoods";
	}
	 @RequestMapping("/uploadGoodsImg.do")  
     @ResponseBody
     public Map<String,Object> method2(@RequestParam("uploadImg") MultipartFile file,HttpServletRequest req) { 
		 Map<String,Object> result = new HashMap<>();
             //多个参数的话只要多个@RequestParam即可，注意参数名要和表单里面的属性名一致
         String m =DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")+new Random().nextInt(100)+10;
        	 String[] fileSplit = file.getOriginalFilename().split("\\.");
        	 String fileSuffix = fileSplit[fileSplit.length-1];
        	 String fileName = m+"."+fileSuffix;
        	 
             String path = req.getSession().getServletContext().getRealPath("upload");  
             System.out.println(path);
             File targetFile = new File(path, fileName);  
             if(!targetFile.exists()){  
                 targetFile.mkdirs();  
             }  
       
             //保存  
             try {
                 file.transferTo(targetFile);  
             } catch (Exception e) {  
                 e.printStackTrace();  
             }  
             result.put("success", true);
             result.put("data", "/upload/"+fileName);
         return result;
     }  
	@RequestMapping("/saveGoods.do")
	@ResponseBody
	public Map<String,Object> saveGoods(GoodsDTO goodsDTO){
		Map<String,Object> result = new HashMap<>();
		try {
			goodsService.saveGoods(goodsDTO);
			result.put("success", true);
			result.put("data", goodsDTO);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}
	
	@RequestMapping("/ajaxLoad.do")
	@ResponseBody
	public Map<String,Object> ajaxLoad(String goodsName){
		List<Goods> cList = goodsService.ajaxLoad(goodsName);
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("success", true);
		resultMap.put("data", cList);
		return resultMap;
	}
}
